In [42]:
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
You've been using classes all of the time, without even knowing it. Everything in Python is an object. You have some data (number, text, etc.)with some methods (or functions) which are internal to the object, and which you can use on that data. Lets look at a few examples...
In [ ]:
How can I see what methods an object of type float has?
In [ ]:
In [ ]:
They're hidden methods - we'll talk more about these later.
Define some key things:
Aside: If you're a C++/Java programmer, 'self' is exactly equivalent to 'this', but functions must have self as an argument, as it is passed in implicitly as the first argument of any method call in Python.
In [ ]:
Use the same syntax as we use to call a function, BUT the arguments get passed in to the "__init__" function. Note that you ignore the self object, as Python sorts this out.
In [ ]:
How do I access data stored in the class? with the ".", followed by the name.
In [ ]:
What happens though if we reuse the variable name 'a'?
Aside:
In [ ]:
So, what happens if we either choose to store something else under the name 'a', or tell Python to delte it?
In [ ]:
In [ ]:
In [ ]:
What information does it need to know about itself?
In [ ]:
What we're going to try and do is add particles to the box, which have some properties:
An initial velocity
$r(t + \delta t) \approx r(t) + v(t)\delta t$
In [ ]:
Lets just check this, if a Particle is in a box of length 10, has r0 = 0, v0=5, then after 1 step of length 3, the position should be at position 5:
In [ ]:
Lets add a place to store the particles to the box class, and add a method to add particles to the box:
In [ ]:
Tasks (30-40 minutes):
1) Add a method that calculates the average position of Particles in the box (Hint: you might have to think about what to do when there are no particles!)
2) Add a method that makes all of the particles step forwards, and keep track of how much time has passed in the box class.
3) Add a method which plots the current position of the particles in the box.
4) Write a method that writes the current positions and velocities to a CSV file.
5) Write a method that can load a CSV file of positions and velocities, create particles with these and then add them to the Box list of particles. (Hint: Look up the documentation for the module 'csv')
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Remember earlier, when we did:
In [ ]:
Notice that there is a method "__add__" - we can define these special methods to allow our class to do things that you can ordinarily do with built in types.
In [ ]:
Now we've created an 'add' method, we can, create two boxes and add these together!
In [ ]:
In [ ]:
In [ ]:
In [ ]:
Why has the mean position of particles in Box C changed? Look at the memory address of the particles:
In [ ]:
Boxes are pointing to the SAME particles! If we don't want this to happen, we need to write a 'copy' constructor for the class - a function which knows how to create an identical copy of the particle!
We can do this by using the 'deepcopy' function in the 'copy' module, and redefine the particle and slow particle classes:
In [ ]:
Then, we should change the Box class's 'add' method, to use this copy operation rather than just append the child particles of the existing boxes:
In [ ]:
In [ ]:
In [ ]:
In [ ]: